package com.itheima.controller;

import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    //查询列表
    @GetMapping("/student")
    public ResultInfo findAll() {
        List<Student> studentList = studentService.findAll();

        //将数据封装到ResultInfo再返回
        return ResultInfo.success(studentList);
    }

    //保存
    @PostMapping("/student")
    public ResultInfo save(@RequestBody Student student) {
        studentService.save(student);
        return ResultInfo.success(null);
    }


    //查询 主要占位符使用的是{}  不是${}
    @GetMapping("/student/{number}")
    public ResultInfo findByNumber(@PathVariable("number") String number){
        Student student = studentService.findByNumber(number);
        return ResultInfo.success(student);
    }

    //@RequestBody:在restful风格中,只有前端通过post和put的请求体传递参数的时候
    //修改
    @PutMapping("/student")
    public ResultInfo update(@RequestBody Student student){
        studentService.update(student);
        return ResultInfo.success(null);
    }

    //删除
    @DeleteMapping("/student/{number}")
    public ResultInfo deleteByNumber(@PathVariable("number") String number){
        studentService.deleteByNumber(number);
        return ResultInfo.success(number);
    }
}
